<?php


namespace App\Admin\Controllers\ApiDoc;

use App\Constant\ContentType;
use App\Constant\ParamType;
use App\Constant\RequestMethod;
use App\Constant\RequestPart;
use App\Http\Controllers\Controller;
use App\Model\Repository\ApiCategoryRepository;
use App\Model\Repository\ApiProjectRepository;
use App\Model\Repository\ApiRepository;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ApiController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('Index');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Show interface.
     *
     * @param $id
     * @return Content
     */
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Detail');
            $content->description('description');

            $content->body(Admin::show(ApiRepository::findOrFail($id), function (Show $show) {
                $show->field('api_id');
                $show->field('api_name', '接口名称');
                $show->field('domain', '域名');
                $show->field('path', '路径');
                $show->field('version', '版本号');
                $show->field('method', '请求方法');
                $show->field('content_type', '请求参数类型');
                $show->field('remark', '备注');
                $show->request('请求参数', function (Grid $grid) {
                    $grid->resource('/admin/apidoc/request_body');
                    $grid->column('param_name', '参数名');
                    $grid->column('param_type', '参数类型');
                    $grid->column('part', '提交位置');
                    $grid->column('remark', '备注');
                });
                $show->response('响应参数', function (Show $show) {
                    $show->setResource('/admin/apidoc/response_body');
                    $show->field('content_type', '提交位置');
                    $show->field('remark', '备注');
                    $show->field('body', '参数名');
                    $show->field('demo', '参数类型');
                });
                $show->project('所属项目', function (Show $show) {
                    $show->setResource('/admin/apidoc/project');
                    $show->field('project_name', '项目名称');
                });
                $show->category('接口分类', function (Show $show) {
                    $show->setResource('/admin/apidoc/api_category');
                    $show->field('category_name', '分类名称');
                });

                $show->field('created_at', '创建时间');
                $show->field('updated_at', '修改时间');
            }));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('Edit');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ApiRepository::class, function (Grid $grid) {

            $grid->column('api_id')->sortable();
            $grid->column('api_name', '接口名称');
            $grid->column('domain', '域名');
            $grid->column('path', '路径');
            $grid->column('version', '版本号');
            $grid->column('method', '请求方法');
            $grid->column('category.category_name', '所属分类');
//            $grid->column('comment', '备注');

            $grid->column('created_at', '创建时间');
            $grid->column('updated_at', '修改时间');
//            $grid->updated_at();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->disableIdFilter();
                $filter->equal('project_id', '所属项目')->select(ApiProjectRepository::getProjectMap());
                $filter->equal('category_id', '接口分类')->select(ApiCategoryRepository::getCategoryMap());
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ApiRepository::class, function (Form $form) {

            $form->display('api_id', '接口id');
            $form->text('api_name', '接口名称');
            $form->text('domain', '域名')->placeholder('请使用变量');
            $form->text('path', '路径');
            $form->text('version', '版本号')->placeholder('不需要v标识');
            $form->textarea('remark', '备注')->default("暂无");
            $form->text('content_type', '请求参数类型')
                ->placeholder('填form-data,x-www-form-urlencoded等')
                ->default("x-www-form-urlencoded");

            $form->select('method', '请求方法')->options(RequestMethod::getSelectOptions())->default('POST');

            $form->select('project_id', '所属项目')->options(ApiProjectRepository::getProjectMap());
            $form->select('category_id', '接口分类')->options(ApiCategoryRepository::getCategoryMap());
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
            $form->select('response.content_type', '响应参数类型')->options(ContentType::getSelectOptions());
            $form->editor('response.body', '响应body参数');
            $form->editor('response.demo', '响应body参数demo');
            $form->textarea('response.remark', '响应参数备注')->default('暂无');
            $form->hasMany('request', '请求参数', function (Form\NestedForm $form) {
                $form->text('param_name', '参数名')->rules('required');
                $form->select('param_type', '参数类型')->options(ParamType::getSelectOptions())->rules('required');
                $form->select('part', '提交位置')->options(RequestPart::getSelectOptions())->default('body');
                $form->text('default', '默认值')->rules('required')->default(0);
                $form->text('remark', '备注')->default('暂无')->rules('required');
            })->value();
        });
    }
}